package com.atguigu.tingshu.user.service.impl;

import com.atguigu.tingshu.common.cache.TsCache;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.mapper.UserPaidTrackMapper;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

	@Autowired
	private UserInfoMapper userInfoMapper;
    @Autowired
    private UserPaidAlbumMapper userPaidAlbumMapper;
    @Autowired
    private UserPaidTrackMapper userPaidTrackMapper;

	@Override
	public UserInfoVo getUserInfo(Long userId) {
		UserInfoVo userInfoVo = new UserInfoVo();
		UserInfo userInfo = this.getById(userId);
		BeanUtils.copyProperties(userInfo,userInfoVo);
		return userInfoVo;
	}

	@Override
	public void updateUser(Long userId, UserInfoVo userInfoVo) {
		UserInfo userInfo = new UserInfo();
		BeanUtils.copyProperties(userInfoVo,userInfo);
		userInfo.setId(userId);
		userInfoMapper.updateById(userInfo);
	}

	@Override
	public Map<Long, Integer> userIsPaidTrack(Long albumId, List<Long> trackIdList, Long userId) {
		Map<Long, Integer> map = trackIdList.stream().collect(Collectors.toMap(trackId -> trackId, trackId -> 0));
		//用户是否购买过专辑
		UserPaidAlbum userPaidAlbum = userPaidAlbumMapper
				.selectOne(new LambdaQueryWrapper<UserPaidAlbum>()
						.eq(UserPaidAlbum::getUserId, userId)
						.eq(UserPaidAlbum::getAlbumId, albumId));
		//已购买
		if (null!=userPaidAlbum){
			return trackIdList.stream().collect(Collectors.toMap(trackId -> trackId, trackId -> 1));
		}
		//根据用户id，需要付款的声音id集合列表查询用户是否购买当前声音
		List<UserPaidTrack> userPaidTracks = userPaidTrackMapper
				.selectList(new LambdaQueryWrapper<UserPaidTrack>()
						.eq(UserPaidTrack::getUserId, userId)
						.in(UserPaidTrack::getTrackId, trackIdList));
		if (!CollectionUtils.isEmpty(userPaidTracks)) {
			for (UserPaidTrack userPaidTrack : userPaidTracks) {
				map.put(userPaidTrack.getTrackId(),1);
			}
		}
		return map;
	}

	@Override
	@TsCache(keyPrefix = "user:info")
	public UserInfoVo getUserInfoVo(Long userId) {
		UserInfoVo userInfoVo = new UserInfoVo();
		UserInfo userInfo = this.getById(userId);
		BeanUtils.copyProperties(userInfo,userInfoVo);
		return userInfoVo;
	}
}
